{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1956967341', 'empty', 'xoshayzers', '@tiffanylue i know  i was listenin to bad habit earlier and i started freakin at his part =[']\n"
     ]
    }
   ],
   "source": [
    "import csv\n",
    "\n",
    "with open('dataset/data/text_emotion.csv', 'r') as f:\n",
    "      reader = csv.reader(f)\n",
    "      data = list(reader)\n",
    "\n",
    "print(data[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "@tiffanylue i know  i was listenin to bad habit earlier and i started freakin at his part =[\n"
     ]
    }
   ],
   "source": [
    "texts = []\n",
    "for i, point in enumerate(data):\n",
    "    texts.append(data[i][3])\n",
    "texts.pop(0)\n",
    "print(texts[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "empty\n"
     ]
    }
   ],
   "source": [
    "labels = []\n",
    "for i, point in enumerate(data):\n",
    "    labels.append(data[i][1])\n",
    "labels.pop(0)\n",
    "print(labels[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "text_train, text_test, label_train, label_test = train_test_split(texts, labels,test_size=0.001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer\n",
    "from sklearn.linear_model import SGDClassifier\n",
    "\n",
    "text_clf = Pipeline([('vect', CountVectorizer(stop_words='english')),\n",
    "                         ('tfidf', TfidfTransformer()),\n",
    "                         ('clf-svm',\n",
    "                          SGDClassifier(loss='hinge',penalty='l2',alpha=1e-3,max_iter=100,tol=1e-6,random_state=42)),])\n",
    "\n",
    "_ = text_clf.fit(text_train, label_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predicted_svm = text_clf.predict(text_test)\n",
    "np.mean(predicted_svm == label_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "What his idea? Copy China and get his supporter to say his idea?\n",
      "['neutral']\n",
      "There are bigger issues to solved and this stolen ideas gets praised. Is million dollar ministers capable of only such ideas??\n",
      "['fun']\n",
      "old people are demanding nowadays, and they ask why their children dumped them.\n",
      "['neutral']\n",
      "your mouth stink.\n",
      "['neutral']\n",
      "thats damn fucked up boi\n",
      "['sadness']\n",
      "i'm going to stuff that up your sorry ass\n",
      "['worry']\n",
      "cb fuck this shit\n",
      "['hate']\n",
      "I hate to be in a train where people #fart and make me feel like going to die. \n",
      "['hate']\n",
      "ok, that went well\n",
      "['worry']\n"
     ]
    }
   ],
   "source": [
    "test_set = [\"What his idea? Copy China and get his supporter to say his idea?\",\n",
    "           \"There are bigger issues to solved and this stolen ideas gets praised. Is million dollar ministers capable of only such ideas??\",\n",
    "           \"old people are demanding nowadays, and they ask why their children dumped them.\",\n",
    "           \"your mouth stink.\",\n",
    "            \"thats damn fucked up boi\",\n",
    "            \"i'm going to stuff that up your sorry ass\",\n",
    "            \"cb fuck this shit\",\n",
    "            \"I hate to be in a train where people #fart and make me feel like going to die. \",\n",
    "            \"ok, that went well\"\n",
    "           ]\n",
    "for text in test_set:\n",
    "    print(text)\n",
    "    print(text_clf.predict([text]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
